﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
using namespace std;

//class MyPrintAdapter
class MyPrintAdapter :public binary_function<int, int, void>
{
public:
	/*void operator()(int v)
	{
		cout << v << endl;
	}*/
	void operator()(int v, int start) const
	{
		//cout << v + start << endl;
		cout << "v= " << v << " ,start= " << start << " ,v+start= " << v + start << endl;
	}
};
class GreaterThanFive :public unary_function<int, bool>
{
public:
	bool operator()(int val)const
	{
		return val > 5;
	}
};
//void myPrintForEach(int v)
void myPrintForEach(int v, int start)
{
	//cout << v << " ";
	cout << v + start << endl;
}
//10.成员函数适配器；
class PersonAdapter
{
public:
	PersonAdapter(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	void showPerson()
	{
		cout << "成员函数中，姓名：" << m_Name << " ，年龄：" << m_Age << endl;
	}

	void plugAge()
	{
		this->m_Age = this->m_Age + 100;
	}

	string m_Name;
	int m_Age;
};
void myPrintPersonAdapter(PersonAdapter& p)
{
	cout << "姓名：" << p.m_Name << " ，年龄：" << p.m_Age << endl;
}
void testAdapter()
{
	vector<int>v;
	for (int i = 0; i < 10; ++i)
	{
		v.push_back(i);
	}

	cout << "请输入起始值：" << endl;
	int num;
	cin >> num;//100

	//for_each(v.begin(), v.end(), MyPrintAdapter());//0,..,9

	//1.第一步，绑定数据，bind2nd；
	//2.第二步，继承类binary_function<参数类型1，参数类型2，返回值类型>；
	//3.第三步，加const修饰operator()，即不能修改值；
	for_each(v.begin(), v.end(), bind2nd(MyPrintAdapter(), num));
	/*v = 0, start = 100, v + start = 100
	v = 1, start = 100, v + start = 101
	v = 2, start = 100, v + start = 102
	v = 3, start = 100, v + start = 103
	v = 4, start = 100, v + start = 104
	v = 5, start = 100, v + start = 105
	v = 6, start = 100, v + start = 106
	v = 7, start = 100, v + start = 107
	v = 8, start = 100, v + start = 108
	v = 9, start = 100, v + start = 109*/
	for_each(v.begin(), v.end(), bind1st(MyPrintAdapter(), num));
	/*v = 100, start = 0, v + start = 100
	v = 100, start = 1, v + start = 101
	v = 100, start = 2, v + start = 102
	v = 100, start = 3, v + start = 103
	v = 100, start = 4, v + start = 104
	v = 100, start = 5, v + start = 105
	v = 100, start = 6, v + start = 106
	v = 100, start = 7, v + start = 107
	v = 100, start = 8, v + start = 108
	v = 100, start = 9, v + start = 109*/

	//4.取反适配器；一元取反：查找大于5的数字；
	//vector<int>::iterator pos = find_if(v.begin(), v.end(), GreaterThanFive());

	//5.需求改为：找小于5的数字；
	//vector<int>::iterator pos = find_if(v.begin(), v.end(), not1(GreaterThanFive()));
	//7.greater<int>()，内建函数对象；比较高端的操作；
	vector<int>::iterator pos = find_if(v.begin(), v.end(), not1(bind2nd(greater<int>(), 5)));
	if (pos != v.end())
	{
		//cout << "找到大于5的数字为：" << *pos << endl;//6
		cout << "找到小于5的数字为：" << *pos << endl;//0 --> 0
	}
	else
	{
		cout << "未找到！" << endl;
	}

	//6.一元取反适配器：not1，继承unary_function<参数类型1，返回值类型>，const修饰；


	//8.函数指针适配器；
	//for_each(v.begin(), v.end(), myPrintForEach);//0 1 2 3 4 5 6 7 8 9

	//9.将函数指针适配为函数对象；ptr_fun
	for_each(v.begin(), v.end(), bind2nd(ptr_fun(myPrintForEach), 100));//100, ... ,109

	vector<PersonAdapter>vP;

	PersonAdapter p1("aaa", 10);
	PersonAdapter p2("bbb", 15);
	PersonAdapter p3("ccc", 18);
	PersonAdapter p4("ddd", 40);

	vP.push_back(p1);
	vP.push_back(p2);
	vP.push_back(p3);
	vP.push_back(p4);

	for_each(vP.begin(), vP.end(), myPrintPersonAdapter);
	/*姓名：aaa ，年龄：10
	姓名：bbb ，年龄：15
	姓名：ccc ，年龄：18
	姓名：ddd ，年龄：40*/
	//11.成员函数适配器，mem_fun_ref
	for_each(vP.begin(), vP.end(), mem_fun_ref(&PersonAdapter::showPerson));
	/*成员函数中，姓名：aaa ，年龄：10
	成员函数中，姓名：bbb ，年龄：15
	成员函数中，姓名：ccc ，年龄：18
	成员函数中，姓名：ddd ，年龄：40*/
	for_each(vP.begin(), vP.end(), mem_fun_ref(&PersonAdapter::plugAge));
	for_each(vP.begin(), vP.end(), mem_fun_ref(&PersonAdapter::showPerson));
	/*成员函数中，姓名：aaa ，年龄：110
	成员函数中，姓名：bbb ，年龄：115
	成员函数中，姓名：ccc ，年龄：118
	成员函数中，姓名：ddd ，年龄：140*/
}

//main333：适配器的用法
int main333()
{
	testAdapter();

	system("pause");
	return EXIT_SUCCESS;
}